package com.dobbinsoft.iotechn.core.util;

import java.util.Random;
import java.util.UUID;

/**
 * Created by rize on 2019/7/1.
 */
public class GeneratorUtil {

    public static String genSixVerifyCode() {
        String time = System.nanoTime() + "";
        return time.substring(time.length() - 6);
    }

    public static String genSessionId() {
        return UUID.randomUUID().toString().replace("-","").toUpperCase();
    }

    /**
     * 随机生成min和max之间一个数，包括min不包括max
     *
     * @param min
     * @param max
     * @return [min, max)
     */
    public static int generateValue(int min, int max) {
        return (int) (new Random().nextDouble() * (max - min) + min);
    }

    public static String genFileName(){
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    public static String genUUId() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    public static String getExtension(String filename) {
        if (filename == null) {
            return null;
        } else {
            int index = indexOfExtension(filename);
            return index == -1 ? "" : filename.substring(index + 1);
        }
    }

    public static int indexOfExtension(String filename) {
        if (filename == null) {
            return -1;
        } else {
            int extensionPos = filename.lastIndexOf(46);
            int lastSeparator = indexOfLastSeparator(filename);
            return lastSeparator > extensionPos ? -1 : extensionPos;
        }
    }

    public static int indexOfLastSeparator(String filename) {
        if (filename == null) {
            return -1;
        } else {
            int lastUnixPos = filename.lastIndexOf(47);
            int lastWindowsPos = filename.lastIndexOf(92);
            return Math.max(lastUnixPos, lastWindowsPos);
        }
    }

}
